t=1485; % t is the period from which one starts the projection
k1=90; % fixed length
step=[1 3 5 9 12 13 14 15 16 17 18 20 22 24 26 28 30 32];
%step=1:2:29; % odd numbered 
%step=1:30; % all 
hor=max(step);
ntimes=length(step);
k2=k1+hor; %
nm=8;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read saved data from eviews
muu=load('mu_all3.txt');
eps=load('eps_all3.txt');
hilos=load('hilos.txt');
sper=length(eps(:,1));
eps_s=sort(eps);
giorni1=load('date_7_18_95.txt');
MATLABDate = x2mdate(giorni1);
giorni=datestr(MATLABDate,2);
giorni1=datestr(MATLABDate,6); 

% load matrices
aplus=load('aplus.txt');   % impact matrix lag 1 overall period dpos
aminus=load('aminus.txt'); % impact matrix lag 1 overall period dneg
acplus=load('acplus.txt');   % impact matrix lag 1 crisis period dpos
acminus=load('acminus.txt'); % impact matrix lag 1 crisis period dneg
ac2=load('a_lag2.txt'); % impact matrix lag 2 
omega1=load('om.txt');
dneg=load('dneg.txt');
beta=load('beta.txt');
ac1=beta+(aplus+aminus)/2;
ad1=(acplus+acminus)/2;

% from here down the same as before
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ge(t,474) & le(t,842) % crisis period
   ac=ac1+ad1;
   om=omega1(:,1)+omega1(:,2);
elseif ge(t,843) % post crisis period
   ac=ac1;
   om=omega1(:,1)+omega1(:,3);
else
   ac=ac1;
   om=omega1(:,1);
end
a=[ac ac2; eye(nm) zeros(nm,nm)];
om=[om ; zeros(nm,1)];
% end of initializations
mu_pr=zeros(nm*2,k2,ntimes+1)*NaN;
for ii=1:ntimes
    it=t+step(ii); % starting period
    st=step(ii);
    %stdat(ii,:)=giorni1(it,:);
    en=st+k1;
    mu_pr(1:nm,st,ii)=muu(it,:)';
    mu_pr(nm+1:nm*2,st,ii)=hilos(it-1,:)';
    mu_pr(:,st+1,ii)=om+a*mu_pr(:,st,ii);
    for ik=st+2:en
        mu_pr(:,ik,ii)=om+a*mu_pr(:,ik-1,ii);
    end
end

mu_pr=mu_pr*log(10)*sqrt(252)*sqrt(pi)/sqrt(8);
%%START GRAPHS 
ctry=['HK';'IN';'KO';'MA';'PH';'SI';'TA';'TH'];
ctry1=[
    'Hong Kong  ';
    'Indonesia  ';
    'Korea      ';
    'Malaysia   ';
    'Philippines';
    'Singapore  ';
    'Taiwan     ';
    'Thailand   '];
mkt_mrr=['h';'o';'*';'^';'+';'p';'s';'p'];
mkt_mrr=['none';'none';'none';'none';'none';'none';'none';'none'];
mkt_col=['k';'r';'g';'b';'k';'m';'y';'c'];
mkt_col=['k';'r';'g';'b';'m'];
mkt_col=['k';'b'];

mkt_sty=['-   ';'-   ';'--  ';':   ';'-.  ';'-   ';'-   ';'-   '];
nt=20;
st=1;
en=k1;
gst=1;
gen=k2;
gstep=round((gen-gst+1)/nt);
gxt=zeros(1,nt+1);
gxt(1,1)=1;
%gg(1,:)=giorni(t+gst,:);
gg(1,:)=giorni1(t+gst,:);
for i=2:nt+1
    gxt(1,i)=gxt(1,i-1)+gstep;
    %gg(i,:)=giorni(t+gxt(1,i),:);
    gg(i,:)=giorni1(t+gxt(1,i),:);
end

tt=1:k2;
for mkt=1:nm
    mf = figure(mkt);
    set(mf,'Color','w')
    %subplot(2,2,1:2); bar(1:k2/2,hilos(t+1:t+k2/2,mkt)','FaceColor','none')
    %subplot(2,2,3:4);
    plot(tt,mu_pr(mkt,:,1),'LineWidth',2,'MarkerSize',4,'Marker',mkt_mrr(1,:),'Color',mkt_col(2,:));
    hold on
    for i=2:ntimes
        ic=mod(i,length(mkt_col))+1; % to cycle over colors 
        plot(tt,mu_pr(mkt,:,i),'LineWidth',2,'MarkerSize',4,'Marker',mkt_mrr(1,:),'Color',mkt_col(ic,:));
    end
    title(['Volatility Forecast: ' ctry(mkt,:),' - Sep. 2001 Episode'] ,'FontSize',24 )
%    title([ctry1(mkt,:)] ,'FontSize',24 )
    %xlabel('Starting Date','FontSize',16)
    ylabel('Forecast','FontSize',16)
    set(gca,'XTick',[ gxt ])
    set(gca,'XTickLabel',{ gg })
    set(gca,'FontSize',16)
    %legend(stdat,'Location','NorthEast')
    ylim([10 50])
    xlim([1 100])
    line([10 10],[10 50],'LineWidth',2,'Color','k','LineStyle',':')
    line([14 14],[10 50],'LineWidth',2,'Color','k','LineStyle',':')
    hold off
    X=getframe(gcf);
    imwrite(X.cdata,[ctry(mkt,:) '_sept01_b.png'],'png','BitDepth',16);
end
